<?php

namespace Guest\Controller;

use Application\Entity\Contact;
use Application\Entity\User;
use Application\Form\UserForm;
use Application\InputFilter\UserFilter;
use Cf\Mvc\Controller\Action;
use Zend\Crypt\Password\Bcrypt;
use Zend\View\Model\ViewModel;

class IndexController extends Action
{

    protected $userTable = null;
    protected $authService;

    public function indexAction()
    {
        return new ViewModel();
    }

    public function newAction()
    {
        $form = new UserForm();
        $form->get('submit')->setValue('Register');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $filter = new UserFilter();
            $data = $request->getPost();
            $form->setInputFilter($filter->getInputFilter());
            $form->setData($data);

            if ($form->isValid()) {
                $em = $this->getServiceLocator()
                    ->get('Doctrine\ORM\EntityManager');
                $bCrypt = new Bcrypt();
                $password = $bCrypt->create($data['password']);
                $user = new User();
                $contact = new Contact();
                $user->setEmail($data['email']);
                $user->setPassword($password);
                $user->setCreated(date_create(date('Y-m-d H:i:s')));
                $user->setContactContact($contact);

                $em->persist($contact);
                $em->persist($user);
                $em->flush();

                return $this->redirect()->toRoute('home');
            }
        }
        return array('form' => $form);
    }

    public function getUserTable()
    {
        if (!$this->userTable) {
            $sm = $this->getServiceLocator();
            $this->userTable = $sm->get('Application\Model\UserTable');
        }
        return $this->userTable;
    }

    public function loginAction()
    {
        if ($this->getFrontAuthService()->hasIdentity()) {
            return $this->redirect()->toRoute('user');
        }
        $form = new UserForm();
        $request = $this->getRequest();

        if ($request->isPost()) {
            $user = new User();
            $filter = new UserFilter();
            $data = $request->getPost();
            $form->setInputFilter($filter->getInputFilter());
            $data['passwordConfirmation'] = $data['password'];
            $form->setData($data);

            if ($form->isValid()) {
                $this->getFrontAuthService()->getAdapter()
                    ->setIdentity($request->getPost('email'))
                    ->setCredential($request->getPost('password'));

                $result = $this->getFrontAuthService()->authenticate();
                foreach ($result->getMessages() as $message) {
                    $this->flashmessenger()->addMessage($message);
                }

                if ($result->isValid()) {
                    if ($request->getPost('rememberme') == 1) {
                        $this->getSessionStorage()
                            ->setRememberMe(1);
                        $this->getFrontAuthService()->setStorage($this->getFrontSessionStorage());
                    }
                    $this->getFrontAuthService()->getStorage()->write($request->getPost('email'));
                    return $this->redirect()->toRoute('user');
                }
            }
        }

        $form->get('submit')->setValue('Login');

        return array(
            'form' => $form,
            'messages' => $this->flashmessenger()->getMessages()
        );
    }



    public function authenticateAction()
    {
        $form = $this->getForm();
        $redirect = 'login';

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());
            if ($form->isValid()) {
                //check authentication...
                $this->getAuthService()->getAdapter()
                    ->setIdentity($request->getPost('email'))
                    ->setCredential($request->getPost('password'));

                $result = $this->getAuthService()->authenticate();
                foreach ($result->getMessages() as $message) {
                    //save message temporary into flashmessenger
                    $this->flashmessenger()->addMessage($message);
                }

                if ($result->isValid()) {
                    $redirect = 'success';
                    //check if it has rememberMe :
                    if ($request->getPost('rememberme') == 1) {
                        $this->getSessionStorage()
                            ->setRememberMe(1);
                        //set storage again
                        $this->getAuthService()->setStorage($this->getSessionStorage());
                    }
                    $this->getAuthService()->getStorage()->write($request->getPost('email'));
                }
            }
        }

        return $this->redirect()->toRoute($redirect);
    }


}

